package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.Map;


import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.JxStudyFileEntity;
import io.renren.modules.sys.service.JxStudyFileService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 学习资料
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2021-10-11 14:39:50
 */
@RestController
@RequestMapping("sys/jxstudyfile")
public class JxStudyFileController {
    @Autowired
    private JxStudyFileService jxStudyFileService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("sys:jxstudyfile:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = jxStudyFileService.getAllWithPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:jxstudyfile:info")
    public R info(@PathVariable("id") Long id){
        JxStudyFileEntity jxStudyFile = jxStudyFileService.getById(id);

        return R.ok().put("jxStudyFile", jxStudyFile);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:jxstudyfile:save")
    public R save(@RequestBody JxStudyFileEntity jxStudyFile){
        ValidatorUtils.validateEntity(jxStudyFile, AddGroup.class);
        String[] text=jxStudyFile.getFileUrl().split("/");
        String tt="";
        if(text[text.length - 1].lastIndexOf(".")>0){
            tt= text[text.length - 1].substring(text[text.length - 1].lastIndexOf("."));
            tt=tt.replace(".","");
        }
        jxStudyFile.setFilePostfix(tt);
        jxStudyFileService.save(jxStudyFile);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:jxstudyfile:update")
    public R update(@RequestBody JxStudyFileEntity jxStudyFile){
        ValidatorUtils.validateEntity(jxStudyFile,UpdateGroup.class);
        jxStudyFileService.updateById(jxStudyFile);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:jxstudyfile:delete")
    public R delete(@RequestBody Long[] ids){
        jxStudyFileService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
